package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/8

*/
public class WebTheadlj implements Runnable{
    String string;
    Document doc;


    WebTheadlj(String str, Document docc){
        string= str;
        doc= docc;
    }

    public static void main(String[] args) throws IOException {
        String proxyHost = "127.0.0.1";
        String proxyPort = "11478";
        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);
        // 对https也开启代理
        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);  //设置代理来访问豆瓣

        String link = "https://nj.lianjia.com/zufang/pg2/#contentList";
        Connection connect = Jsoup.connect(link);
        Document doc = connect.get();

        Elements elements = doc.getElementsByClass("content__list--item");
        for (Element element : elements) {
            System.out.println(element);
        }

    }

    private static Logger logger = Logger.getLogger(WebTheadlj.class);

    synchronized public void run() {

            Elements listCon = doc.getElementsByClass("content__list--item");
        String imgUrl,houseSingleUrl,title,singleType,size,direcType,singleLayer,rentType,place,underPlace,price;
        for (Element element : listCon) {
            try {
                Thread.sleep(2000);
            //获取房子的图片链
                imgUrl = element.getElementsByTag("img").attr("data-src");
            //获取房子的具体信息链接
             houseSingleUrl = "https://nj.zu.ke.com"+element.getElementsByClass("twoline").attr("href");
           //房子的标题
             title = element.getElementsByTag("a").attr("title");

            String replace = Html2Text.getContent(element.getElementsByClass("content__list--item--des").html());
            String[] info = replace.split("/");
            //获取房型：1室2厅
             singleType = "";
            //获取面积
             size = "";
            //获取精装，简装
             direcType = "";
            //获取楼层
             singleLayer = "";
            //整租，合租，出租类型
             rentType = "";
            //获取房型
             place ="";

            if (info.length == 6){
                singleType = info[4];  size = info[2];   singleLayer = info[5];place = info[1];
            }else{
                singleType = info[3];  size = info[1];   singleLayer = info[4];place = info[0];
            }
            direcType = element.getElementsByClass("content__item__tag--decoration").html();

            rentType = title.split("·")[0];


            //秦淮-月牙湖-天坛新村^空房
            Thread.sleep(2000);
            //南京市月牙湖小学
            underPlace = "暂无信息";
            //房子的价格
             price = element.getElementsByClass("content__list--item-price").get(0).child(0).html();
            }catch (InterruptedException | IndexOutOfBoundsException exception) {
                continue;
            }
            //输出信息到日志
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(imgUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(houseSingleUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(title.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(size.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(direcType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleLayer.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(rentType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(place.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(underPlace.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(price.replace("\t","").replace("\r","").replace("\n",""));
            logger.info(stringBuilder.toString());
        }




    }
}
